package cn.com.controller;

import cn.com.pojo.User;
import cn.com.service.UserService;
import com.alibaba.fastjson.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/users")
public class UserController {
    @Resource
    private UserService userService;

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ResponseBody
    public Object getByAN(@RequestParam("accountNumber") String accountNumber){
        User user = userService.getByAN(accountNumber);
        return JSONArray.toJSONString(user);
    }

    @RequestMapping(value = "/addUser",method = RequestMethod.POST)
    @ResponseBody
    public Object add(User user){
        String msg = "";
        if (userService.add(user)>0){
            msg = "成功!";
        }else{
            msg = "失败!";
        }
        return JSONArray.toJSONString(msg);
    }

    @RequestMapping(value = "/updateUser",method = RequestMethod.POST)
    @ResponseBody
    public Object update(User user){
        String msg = "";
        if (userService.update(user)>0){
            msg = "成功!";
        }else{
            msg = "失败!";
        }
        return JSONArray.toJSONString(msg);
    }
}
